<?php

// +----------------------------------------------------------------------
// | Date: 2018-05-04
// +----------------------------------------------------------------------
// | Author: zj 
// +----------------------------------------------------------------------
// | 服务器数据获取 
// +----------------------------------------------------------------------

namespace app\game\api;
use think\Db;
class SystemApi{
    private $serverId;
    public function __construct($id) {
        $this->serverId = $id;
    }
    //获取数据，服务器密钥
    public function getDbMes($way,$post_data){
        if(empty($post_data['model'])){
            return false;
        }
        if(!empty($post_data['where'])){
            $post_data['where'] = json_encode($post_data['where']);
        }
        $id = $this->serverId;
    	$web = Db::name('server')->where(['id'=>$id])->find();
    	return $this->excute_p($web,$way,$post_data);
    }
    public function alterDbMes($way,$post_data){
        if(empty($post_data['data'])){
            return false;
        }
        if(!empty($post_data['where'])){
            $post_data['where'] = json_encode($post_data['where']);
        }
        $post_data['data'] = json_encode($post_data['data']);
        $id = $this->serverId;
    	$web = Db::name('server')->where(['id'=>$id])->find();
    	return $this->excute_p($web,$way,$post_data);
    }
    //发送请求
    private function excute($web,$act){
    	$url = $web['url_s'];
    	if($web['type_i']==1){
    		$data = $this->getCurl('http://'.$url.'/i.php?sign='.$web['security_s'].'&act='.$act);
    		$d = json_decode($data,true);
    		if($d && isset($d['data'])){
    			$data = $d['data'];
    		}
    	}else{
    		$data = $this->getCurl('http://'.$url.'/index.php/inner/inner/go?sign='.$web['security_s'].'&func='.$act);
    	}
    	return $data;
    }
    private function excute_p($web,$act,$post_data){
        $url = $web['url_s'];
        $post_data['sign']= $web['security_s'];
        $post_data['act'] = $act;
        $data = $this->postCurl('http://'.$url.'/i.php', $post_data);
        $d = json_decode($data,true);
        if($d && isset($d['data'])){
            $data = $d['data'];
        }
    	return $data;
    }
    /**
    * 获取curl
    * @param int $coin
    * @return 获取到的信息
    */
    public function getCurl($url,$params = array()) {
        if($params){
            $paramStr = http_build_query($params, '', '&');
            if($paramStr){
                if(strpos($url,'?')===false){
                    $url = $url.'?'.$paramStr;
                }else{
                    $url = $url.'&'.$paramStr;
                }
            }
        }
        // 	echo $url;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $resp = curl_exec($curl);
        curl_close($curl);
        return $resp;
    }
    public function postCurl($url,$post_data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //设置为POST
        curl_setopt($ch, CURLOPT_POST, 1);
        //把POST的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);
        return  $output;
    }
}


